package dicky.fortunetelling;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;


import dicky.fortunetelling.logic.BaZi;
import dicky.fortunetelling.logic.DataArray;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Saidbone extends Activity {
	private Button btn1;
	private EditText date1,time1;
	private TextView text1,text2,text3,text4;
	private Calendar cal = Calendar.getInstance();
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat tf = new SimpleDateFormat("HH:mm");
	
	private String tag=Saidbone.class.getSimpleName();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saidbone);
        
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				saidbonesearch();
			}
		});
        
        date1=(EditText)findViewById(R.id.date1);
        date1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				DatePickerDialog dg = new DatePickerDialog(
						Saidbone.this, datelistener, cal
								.get(Calendar.YEAR), cal.get(Calendar.MONTH),
						cal.get(Calendar.DAY_OF_MONTH));
				dg.setTitle("选择日期");
				dg.setButton(DatePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});

        time1=(EditText)findViewById(R.id.time1);
        time1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				TimePickerDialog dg = new TimePickerDialog(Saidbone.this,
						timelistener, cal.get(Calendar.HOUR_OF_DAY), cal
								.get(Calendar.MINUTE), true);
				dg.setTitle("选择时间");
				dg.setButton(TimePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});
        
        text1=(TextView)findViewById(R.id.text1);
        text2=(TextView)findViewById(R.id.text2);
        text3=(TextView)findViewById(R.id.text3);
        text4=(TextView)findViewById(R.id.text4);
    }

	private DatePickerDialog.OnDateSetListener datelistener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, monthOfYear);
			cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			
			date1.setText(df.format(cal.getTime()));
		}
	};
	
	private TimePickerDialog.OnTimeSetListener timelistener = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
			cal.set(Calendar.MINUTE, minute);
			
			time1.setText(tf.format(cal.getTime()));
		}
	};
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (getCurrentFocus() != null
					&& getCurrentFocus().getWindowToken() != null) {
				manager.hideSoftInputFromWindow(getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);

	}
	
	private void saidbonesearch() {
		try {
			String d = date1.getText().toString();
			String t = time1.getText().toString();
			if ("".equals(d) || "".equals(t)) {
				Toast.makeText(Saidbone.this, "请录入出生日期及时间", Toast.LENGTH_LONG).show();
				return;
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(df.parse(d));
			int h = Integer.valueOf(t.substring(0, 2));
			int hour = 1;
			if (h >= 1) {
				hour = 2;
			}
			if (h >= 3) {
				hour = 3;
			}
			if (h >= 5) {
				hour = 4;
			}
			if (h >= 7) {
				hour = 5;
			}
			if (h >= 9) {
				hour = 6;
			}
			if (h >= 11) {
				hour = 7;
			}
			if (h >= 13) {
				hour = 8;
			}
			if (h >= 15) {
				hour = 9;
			}
			if (h >= 17) {
				hour = 10;
			}
			if (h >= 19) {
				hour = 11;
			}
			if (h >= 21) {
				hour = 12;
			}
			if (h >= 23) {
				hour = 1;
				cal.add(cal.DATE, 1);
			}
			
			BaZi lunar = new BaZi(cal);
			String lunarstr=lunar.toString();
			String ganzhi=lunar.getYearGanZhi(hour);
			String year1=ganzhi.substring(0,2);
			String month1=lunarstr.substring(lunarstr.indexOf("年")+1,lunarstr.indexOf("月")+1);
			String day1=lunarstr.substring(lunarstr.indexOf("月")+1,lunarstr.length());
			String hour1=ganzhi.substring(ganzhi.length()-1, ganzhi.length())+"时";
			double weight=0;
			
			HashMap yearmap=DataArray.GetYearWeight(year1);
			HashMap monthmap=DataArray.GetMonthWeight(month1);
			HashMap daymap=DataArray.GetDayWeight(day1);
			HashMap hourmap=DataArray.GetHourWeight(hour1);
			
			weight+=Double.parseDouble(yearmap.get("num")+"");
			weight+=Double.parseDouble(monthmap.get("num")+"");
			weight+=Double.parseDouble(daymap.get("num")+"");
			weight+=Double.parseDouble(hourmap.get("num")+"");
			weight=Math.round(weight*10)/10.0;
			
			HashMap saidbonemap=DataArray.GetSaidBone(weight+"");
			String str=year1+","+month1+","+day1+","+hour1+","+weight;
			
			Log.d(tag, str);
			Log.d(tag, saidbonemap.toString());
			text1.setText(lunarstr+hour1);
			text2.setText(saidbonemap.get("title")+"");
			text3.setText(saidbonemap.get("text1")+"");
			text4.setText(saidbonemap.get("text2")+"");
			
		} catch (Exception e) {
			Log.e(tag, e+"");
			Toast.makeText(Saidbone.this, "称骨查询失败", Toast.LENGTH_LONG).show();
		}
	}
	
}
